import PanelMgr from "./view/PanelMgr";
import LayerMgr from "./view/LayerMgr";
import Alert from "./alert/Alert";
import TipMgr from "./tip/TipMgr";
import Wait from "./wait/Wait";
import AudioMgr from "./manager/AudioMgr";
import EventMgr from "./manager/EventMgr";


export default class mi {
    // 主 UI
    public static uiMain: string = 'UIMain';
    // 音乐音效路径
    public static soundPath: string = 'sounds';
    // 按钮点击音效
    public static buttonSound: string = 'click';

    // 浏览器参数
    private static _args: { [key: string]: string } = {};
    public static get args() { return this._args; }

    // UI根对象
    private static _uiRoot: cc.Node = null;
    public static get UIRoot(): cc.Node { return this._uiRoot; }


    /**
     * 初始化框架
     * @param uiRoot UI根对象
     */
    public static init(uiRoot: cc.Node): void {
        cc.game.addPersistRootNode(uiRoot);

        this._args = this.parseArgs();
        this._uiRoot = uiRoot;

        AudioMgr.init();
        LayerMgr.init();
        PanelMgr.init();
        TipMgr.inst;
        Alert.inst;
        Wait.inst;
    }

    // 每帧执行
    public static update(dt: number): void {
        EventMgr.inst.update(dt);
    }

    // 复制函数
    public static copy(str: any): void {
        const save = (e) => {
            e.clipboardData.setData('text/plain', str);
            e.preventDefault();
        }
        
        document.addEventListener('copy', save);
        document.execCommand('copy');
        document.removeEventListener('copy', save);
    }


    // 解析路径参数
    private static parseArgs() {
        const params = {};

        if (window.location == null) return params;

        let href = window.location.href;
        const argsStr = href.substr(href.indexOf("?") + 1);
        const argsArr = argsStr.split("&");

        let idx: number, name: string, value: string;

        for (let i = 0; i < argsArr.length; i++) {
            idx = argsArr[i].indexOf("=");
            if (idx <= 0) continue;

            name = argsArr[i].substring(0, idx);
            value = argsArr[i].substr(idx + 1);
            params[name] = value;
        }

        return params;
    }
}
